package com.zc.mall.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zc.mall.enums.ResponseEnum;
import lombok.Data;
import org.springframework.validation.BindingResult;

import java.util.Objects;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName ResponseVo.java
 * @Description 统一返回Vo
 * @createTime 2021年06月03日 11:36:00
 */
@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class ResponseVo <T> {

    private Integer status;

    private String msg;

    private T data;

    public ResponseVo(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ResponseVo(Integer status, T data) {
        this.status = status;
        this.data = data;
    }

    public static <T>ResponseVo<T> successByMsg(String msg) {

        return new ResponseVo<>(ResponseEnum.SUCCESS.getCode(), msg);
    }

    public static <T>ResponseVo<T> success(T data) {

        return new ResponseVo<>(ResponseEnum.SUCCESS.getCode(), data);
    }




    public static <T>ResponseVo<T> success() {

        return new ResponseVo<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
    }


    public static <T>ResponseVo<T> error(ResponseEnum responseEnum) {

        return new ResponseVo<>(responseEnum.getCode(), responseEnum.getMsg());
    }

    public static <T>ResponseVo<T> error(ResponseEnum responseEnum,String msg) {

        return new ResponseVo<>(responseEnum.getCode(), msg);
    }

    public static <T>ResponseVo<T> error(ResponseEnum responseEnum,BindingResult bindingResult) {

        return new ResponseVo<>(responseEnum.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getField() + ""
                + bindingResult.getFieldError().getDefaultMessage());
    }



}
